package com.iran.protoType.javaCloneable;

/**
 * 浅拷贝 仅仅是拷贝引用，并不是赋值；clone是浅拷贝。重写
 * 深拷贝 根据原对象指向的的复杂类型，创建一个新的相同的值，然后赋值
 */
public class Test {
    public static void main(String[] args) throws CloneNotSupportedException {
        Person person = new Person();
        person.setName("yilang");
        person.setSex("男");
        person.setAge(28);
        Person person1 = (Person) person.clone();
        System.out.println(person==person1);
        System.out.println("personName:"+person.getName());
        System.out.println("person1Name:"+person1.getName());
        System.out.println(person.getName()==person1.getName());
        System.out.println(person.getAge()==person1.getAge());
        person1.setName("jiangyejia");
        System.out.println(person.getName());
        System.out.println(person1.getName());
        System.out.println("如何实现深拷贝？复杂对象也继承Cloneable标识接口并重写clone方法就好了。");
        Address address = new Address("家", "XX大街");

        person.setAddress(address);
        Person person2 = (Person)person.clone();
        System.out.println("两个对象是否相等:"+(person == person2));
        System.out.println(person2.getAddress().getType());
        System.out.println("修改person2的地址的type");
        person2.getAddress().setType("家2");
        System.out.println(person.getAddress().getType());
        System.out.println(person2.getAddress()==person.getAddress());
    }
}
